<?php

namespace app\admin\controller\setting;

use app\admin\model\UM as UM;
use app\admin\service\SettingService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\Request as Request;

/**
 * Setting 控制器
 * Class Setting
 * @package app\admin\controller\setting;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class Setting
{
    // 请求参数
    private Request $request;

    // service
    private SettingService $service;

    public function __construct(Request $request, SettingService $settingService)
    {
        $this->request = $request;
        $this->service = $settingService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /setting/setting/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $page, $page_size));
    }

    /**
     * 详情
     * @param UM|null $user
     * @return ApiResponse
     * @url /setting/setting/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $key = $request->param('key', '');

        return ApiResponse::success($this->service->read($user, $key));
    }

    /**
     * 新增|修改
     * @param UM $user
     * @return ApiResponse
     * @url /setting/setting/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        $data = $this->service->modify($user, $request->param('id'), $request->param());

        return ApiResponse::success($data);
    }

}
